package com.OrderingServer.Controller.seller;

import com.OrderingServer.pojo.ProductCategory;
import com.OrderingServer.service.ProductCategoryService;
import com.OrderingServer.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "餐饮类别接口")
@RestController
@ApiResponses({
        @ApiResponse(code = 200,message = "新增成功" ,response = ProductCategory.class),
        @ApiResponse(code = 400,message = "参数错误" ),
        @ApiResponse(code = 403,message = "权限被禁止" ),
        @ApiResponse(code = 500,message = "服务器内部错误" ),
})


@RequestMapping("/seller/ProductCategory")
public class ProductCategoryController {
    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation("查询所有餐饮类别")
    @GetMapping("/list")
    public Result list(){
        return Result.success(productCategoryService.list(new QueryWrapper<ProductCategory>().eq("del",0)));
    }

    @ApiOperation("分页查询所有餐饮类别")
    @GetMapping("selectPage")
    public Result getAllByPage(@RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "5")Integer pageSize,
                               ProductCategory productCategory){
        PageInfo<ProductCategory> pageInfo =productCategoryService.selectPage(pageNum,pageSize,productCategory);
      return   Result.success(pageInfo);
    }
    @ApiOperation("新增餐饮类别")
    @PostMapping("/insert")
    public Result insert(@RequestBody ProductCategory productCategory){
        //清除缓存
        redisTemplate.delete("productVOList");
        return new Result().success(productCategoryService.save(productCategory));
    }
    @ApiOperation("根据id更新餐饮类别")
    @PutMapping("/update")
    public Result update( @RequestBody ProductCategory productCategory){
        //清除缓存
        redisTemplate.delete("productVOList");
        return new Result().success(productCategoryService.updateById(productCategory));
    }
    @ApiOperation("根据id删除餐饮类别")
    @DeleteMapping("delete/{id}")
    public Result delete( @PathVariable("id") Integer categoryId){
        //清除缓存
        redisTemplate.delete("productVOList");
        ProductCategory productCategory =new ProductCategory(categoryId,"1");
        return new Result().success(productCategoryService.updateById(productCategory));
    }
}
